<?php
include ("../jpgraph/jpgraph.php");
include ("../jpgraph/jpgraph_pie.php");
include ("../jpgraph/jpgraph_pie3d.php");
include ("../jpgraph/jpgraph_bar.php");
require "server.php";
# chs=250x100&chd=t:60,40&cht=p3&chl=Hello|World


$chart = jpChartClassFactory::getChart($_REQUEST);
if ( ($chart instanceof jpPieChart) || ($chart instanceof jpPie3DChart) ) {
	$data = $chart->chart_data->first_set;
	$graph = new PieGraph($chart->chart_size->width, $chart->chart_size->height, "auto");
	$graph->SetShadow();
	
	#$graph->title->Set("Hello");
	if ( isset($chart->chart_title[0]) ) {
		$graph->title->Set($chart->chart_title[0]);
		$graph->title->SetFont(FF_FONT1,FS_BOLD);
	}

	$p1 = null;
	if ( $chart instanceof jpPie3DChart ) {
		$p1 = new PiePlot3D($data);
		$p1->SetAngle(50);
	}
	else {
		$p1 = new PiePlot($data);
	}
	
	$aType = PIE_VALUE_ADJPER;
	$p1->SetValueType($aType);
	$p1->SetLabelType($aType);
	$p1->SetSize(0.5);
	$p1->SetCenter(0.45);
	
	$p1->SetLabelType(PIE_VALUE_PER);
	$lbl = array();
	foreach ( $chart->chart_legend as $legent_val ) {
		$lbl[] = "$legent_val\n(%.1f%%)";
	}
	$p1->SetLabels($lbl, 1); 	
	
	$p1->SetLegends( $chart->chart_legend );

	$graph->Add($p1);
	$graph->Stroke();
}
elseif ( $chart instanceof jpBarChart ) {
	$datasets = $chart->chart_data->datasets;
	$graph = new Graph($chart->chart_size->width, $chart->chart_size->height, "auto");
    $graph->SetScale("textlin");
    $graph->SetShadow();

	#$graph->title->Set("Hello");
	if ( isset($chart->chart_title[0]) ) {
		$graph->title->Set($chart->chart_title[0]);
		$graph->title->SetFont(FF_FONT1,FS_BOLD);
	}
    if ( !is_null($chart->axes) && !empty($chart->axes->list) ) {
        $axes = $chart->axes->list;
        foreach ( $axes as $axis ) {
            switch ( $axis->type ) {
            case 'x':
                $graph->xaxis->SetTickLabels($axis->labels);
                break;
            case 'y':
                $graph->yaxis->SetTickLabels($axis->labels);
                break;                
            }
        }
    }    

    $barsets = array();
    $i = 0;
    foreach( $datasets as $dataset ) {
        $bar = new BarPlot($dataset);
        # $bar->SetShadow();
        if ( isset($chart->colors->colorsets[$i]) ) {
            $hex_color = sprintf("#%x", $chart->colors->colorsets[$i]);
            $bar->SetFillColor($hex_color);
        }

        $barsets[] = $bar;
        $i++;
    }

	$gbarplot = new GroupBarPlot($barsets);
    $gbarplot->SetWidth(0.6);
    /*
	$lbl = array();
	foreach ( $chart->chart_legend as $legent_val ) {
		$lbl[] = "$legent_val\n(%.1f%%)";
	}
	$p1->SetLabels($lbl, 1); 	
	
	$p1->SetLegends( $chart->chart_legend );
    */
    if ( $chart->chart_type == "bhs" || $chart->chart_type == "bhg") {
        $graph->Set90AndMargin();
    }
	$graph->Add($gbarplot);
	$graph->Stroke();
}

?>
